package uit.edu.vn.Utility;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Activity.R;
import uit.edu.vn.Model.myFile;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter{
	
	private Context context;
	private List<myFile> fileList;
	private LayoutInflater mInflater;

	private ArrayList<Boolean> itemFileChecked = new ArrayList<Boolean>();
	public FileListAdapter(Context context , List<myFile> fileList) 
	{
		// TODO Auto-generated constructor stub
		
		this.context=context;
		this.fileList=fileList;
		mInflater= LayoutInflater.from(context);
		// thiêt lap mac dinh itemcheck là false
		for (int i = 0; i <this.fileList.size(); i++) 
		{
			File tam=new File(fileList.get(i).getPath());
		
			if(tam.isFile())
			{
				itemFileChecked.add(i, true);
				Log.d("tam", tam.getName());
			}
			else
			{
				itemFileChecked.add(i,false);
				
			}
		}
	}
	@Override
	public int getCount() 
	{
		// TODO Auto-generated method stub
		return fileList.size();
	}
	@Override
	public Object getItem(int position) 
	{
		// TODO Auto-generated method stub
		return fileList.get(position);
	}
	 @Override
	public long getItemId(int position) 
	 {
		// TODO Auto-generated method stub
		return position;
	}
	 @Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	 {
		 final myFile entry = fileList.get(position); 
		 // TODO Auto-generated method stub
		 if(convertView==null)
		 {
			 convertView=mInflater.inflate(R.layout.row_file, null);
		 }
		 TextView txt_nameFile = (TextView)convertView.findViewById(R.id.txt_fileName);
		 TextView txt_sizeFile= (TextView)convertView.findViewById(R.id.txt_filePath);
		 ImageView img_iconFile= (ImageView)convertView.findViewById(R.id.img_fileIcon);
		 txt_nameFile.setText(entry.getName());
		
        	
        	
		 txt_sizeFile.setText(entry.getSize());
		 //set icon 
		 img_iconFile.setImageDrawable(entry.getIcon());	
		// File file=new File()
		 //xy lu check box 
		 CheckBox cbox= (CheckBox)convertView.findViewById(R.id.chkSelectFile);
		 cbox.setOnClickListener(new OnClickListener() {
		        public void onClick(View v) {
		            CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelectFile);

		            if (cb.isChecked()) 
		            {
		               // itemFileChecked.set(position, true);
		            	entry.setChecked(true);
	
		            } else if (!cb.isChecked()) 
		            {
		                //itemFileChecked.set(position, false);
		            	entry.setChecked(false);
		            }
		        }
		    });

		cbox.setChecked(entry.getChecked());
		if(itemFileChecked.get(position)==false)
			{
		
				cbox.setVisibility(convertView.INVISIBLE);
			}
			else
			{
				cbox.setVisibility(convertView.VISIBLE);
					
			}
		
		return convertView;
	}
	public ArrayList<Boolean> getCheckList()
	{
		return itemFileChecked;
	}
	public ArrayList<Boolean> setCheckList(ArrayList<Boolean> itemCheck)
	{
		itemFileChecked=itemCheck;
		return itemFileChecked;
	}
}
